package cn.lbd.arithmetic.leetcode.editor.cn;
//编写一个函数来查找字符串数组中的最长公共前缀。
//
// 如果不存在公共前缀，返回空字符串 ""。 
//
// 示例 1: 
//
// 输入: ["flower","flow","flight"]
//输出: "fl"
// 
//
// 示例 2: 
//
// 输入: ["dog","racecar","car"]
//输出: ""
//解释: 输入不存在公共前缀。
// 
//
// 说明: 
//
// 所有输入只包含小写字母 a-z 。 
// Related Topics 字符串 
// 👍 1243 👎 0


import com.sun.deploy.util.StringUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution14 {
    /*public static String longestCommonPrefix(String[] strs) {
        StringBuilder builder = new StringBuilder();
        boolean flag = false;
        for (int i = 0; i < strs.length; i++) {
            int j = 0;
            while (String.valueOf(strs[i].charAt(j)) != null) {
                for (int i1 = 0; i1 < strs.length; i1++) {
                    if (j >= strs[i1].length()) {
                        flag = true;
                        break;
                    }
                    builder.append(strs[i1].charAt(j));
                }
                if (flag) {
                    break;
                }
                builder.append(",");
                j++;
            }
            if (flag) {
                break;
            }
        }
        flag = false;
        String[] split = builder.toString().split(",");
        StringBuilder builder1 = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            if (split[i].length() != strs.length){
                flag = true;
            }
            String temp = String.valueOf(split[i].charAt(0));
            for (int i1 = 1; i1 < split[i].length(); i1++) {
                if (!temp.equals(String.valueOf(split[i].charAt(i1)))){
                    boolean equals = temp.equals(String.valueOf(split[i].charAt(i1)));
                    flag = true;
                }
            }
            int tmpLength = builder1.length();
            if (!flag){

            if (tmpLength == builder1.length()){
                break;
            }
        }
        return builder1.toString();
    }*/



    /**
     * 纵向遍历
     * */
    public static String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0){
            return "";
        }

        int length = strs[0].length();
        int count = strs.length;
        for (int i = 0; i < length; i++) {
            //每列的第一个数，作为每次的比较标准
            int celHeadNum = strs[0].charAt(i);
            //从每列的第二个数开始比较
            for (int j = 1; j < count; j++) {
                if (i == strs[j].length() || strs[j].charAt(i) != celHeadNum){
                    //截取0到前一位
                    return strs[0].substring(0,i);
                }
            }
        }
        //正常都相等的情况    [aaa,aaa,aaa]
        return strs[0];
    }

    public static void main(String[] args) {
        String[] strs = new String[]{"flower","flow","flight"};
        System.out.println(longestCommonPrefix(strs));
    }
}
//leetcode submit region end(Prohibit modification and deletion)
